package com.struts2.action;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.*;
import com.paypal.*;
import com.struts2.model.CartItem;
import com.struts2.service.PayPalService;

public class PayPalController extends ActionSupport implements SessionAware{

	private static final long serialVersionUID = 1L;
	private PayPalResult result = new PayPalResult();
	private PayPalService payPalService = new PayPalService();
	
	// For SessionAware
	private Map<String, Object> session;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public PayPalResult getResult() {
		return result;
	}

	public void setResult(PayPalResult result) {
		this.result = result;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String paypalsuccess() throws ParseException {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		PayPalSucess payPalSucess = new PayPalSucess();
		this.result = payPalSucess.getPayPal(request);
		List<CartItem> cart = (List<CartItem>) session.get("cart");
		payPalService.insertInvoice(result, cart);
		
		return SUCCESS;
	}

}
